package main

import (
	"context"
	"distributed/grades"
	"distributed/log"
	"distributed/registry"
	"distributed/service"
	"fmt"
	stdlog "log"
)

func main() {
	host, port := "localhost", "6000"
	serviceURL := fmt.Sprintf("http://%s:%s", host, port)

	reg := registry.Registration{
		ServiceName: registry.GradingService,
		ServiceURL:  serviceURL,
		RequiredServices: []registry.ServiceName{
			registry.LogService,
		},
		ServiceUpdateURL: serviceURL + "/services",
		HeartbeatURL:     serviceURL + "/heartbeat",
	}

	ctx, err := service.Start(
		context.Background(),
		reg,
		host,
		port,
		grades.RegisterHandlers,
	)
	if err != nil {
		stdlog.Fatal(err)
	}

	logProvider, err := registry.GetProvider(registry.LogService)
	if err == nil {
		fmt.Printf("Logging service found at: %s\n", logProvider)
		log.SetClientLogger(logProvider, reg.ServiceName)
	}

	<-ctx.Done()
	fmt.Println("Shutting down grading service")
}
